import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: '/layout'
  // },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
  {
    path: '/',
    name: 'layout',
    redirect: '/index',
    component: () => import('@/views/layout/LayoutViews.vue'),
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/layout/IndexViews.vue')
      },
      {
        path: '/kind',
        name: 'kind',
        component: () => import('@/views/layout/KindViews.vue')
      },
      {
        path: '/shoppingcart',
        name: 'shoppingcart',
        component: () => import('@/views/layout/ShoppingCartViews.vue')
      },
      {
        path: '/my',
        name: 'my',
        component: () => import('@/views/layout/MyViews.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/IndexViews.vue')
  },
  {
    path: '/pay',
    name: 'pay',
    component: () => import('@/views/pay/PayViews.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/search/IndexViews.vue')
  },
  {
    path: '/searchList',
    name: 'searchList',
    component: () => import('@/views/search/SearchListViews.vue')
  },
  {
    path: '/prodetail/:id',
    name: 'prodetail',
    component: () => import('@/views/prodetail/ProdetailViews.vue')
  },
  {
    path: '/myorder',
    name: 'myorder',
    component: () => import('@/views/order/IndexViews.vue')
  }

]

const router = new VueRouter({
  routes
})

// 禁止游客访问的路径
const authUrl = ['/pay', '/myorder']

router.beforeEach((to, from, next) => {
  // 1. to 往哪里去， 到哪去的路由信息对象
  // 2. from 从哪里来， 从哪来的路由信息对象
  // 3. next() 是否放行
  // 如果next()调用，就是放行
  // next(路径) 拦截到某个路径页面
  const token = store.state.user.userInfo.token
  console.log('token:', token)
  if (!authUrl.includes(to.path)) {
    next()
    return
  }

  if (token) {
    next()
    console.log('11111')
  } else {
    console.log('22222')
    next('/login')
  }
})

export default router
